package cn.edu.buaa.etei.native2ascii;

import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.jdt.core.IJavaProject;
import org.osgi.framework.BundleContext;

public class Activator extends Plugin {
	private static Activator instance;

	public Activator(){
		super();
		instance = this;
	}
	
	public static Activator getDefault(){
		return instance;
	}
	
	@Override
	public void start(BundleContext context) throws Exception{
		super.start(context);
	}

	static public boolean builderEnabled(IJavaProject project) throws CoreException{
		IProjectDescription desc = project.getProject().getDescription();
		ICommand[] comms = desc.getBuildSpec();
		for(ICommand comm : comms){
			if(PropertiesBuilder.BUILDER_NAME.equals(comm.getBuilderName())){
				return true;
			}
		}
		return false;
	}
	
	static public void addBuilder(IJavaProject project) throws CoreException {
		IProjectDescription desc = project.getProject().getDescription();
		ICommand[] comms = desc.getBuildSpec();
		int javaBuilderPosition = 0;
		for(int i = 0; i < comms.length; i++){
			ICommand comm = comms[i];
			if(PropertiesBuilder.BUILDER_NAME.equals(comm.getBuilderName())){
				return;
			} else if(PropertiesBuilder.JAVA_BUILDER_NAME.equals(comm.getBuilderName())){
				javaBuilderPosition = i;
			}
		}
		ICommand newComms[] = new ICommand[comms.length + 1];
		int native2asciiBuilderPosition = javaBuilderPosition + 1;
		
		System.arraycopy(comms, 0, newComms, 0, native2asciiBuilderPosition);
		System.arraycopy(comms, native2asciiBuilderPosition, newComms, javaBuilderPosition + 2, comms.length - javaBuilderPosition -1);
		newComms[native2asciiBuilderPosition] = desc.newCommand();
		newComms[native2asciiBuilderPosition].setBuilderName(PropertiesBuilder.BUILDER_NAME);
		desc.setBuildSpec(newComms);
		project.getProject().setDescription(desc, null);
	}
	
	static public void removeBuilder(IJavaProject project) throws CoreException {
		IProjectDescription desc = project.getProject().getDescription();
		ICommand[] comms = desc.getBuildSpec();
		ICommand newComms[] = new ICommand[comms.length - 1];
		int native2AsciiCount = 0;
		for(int i = 0; i < comms.length; i++){
			ICommand comm = comms[i];
			if(PropertiesBuilder.BUILDER_NAME.equals(comm.getBuilderName())){
				native2AsciiCount++;
			} else if(newComms.length > 0){
				newComms[i - native2AsciiCount] = comm;
			}
		}
		switch (native2AsciiCount) {
		case 0:
			return;
		case 1:
			desc.setBuildSpec(newComms);
			break;
		default:
			ICommand[] clearedComms = new ICommand[comms.length - native2AsciiCount];
			System.arraycopy(newComms, 0, clearedComms, 0, comms.length - native2AsciiCount);
			desc.setBuildSpec(clearedComms);
			break;
		}
		project.getProject().setDescription(desc, null);
	}

	
	@Override
	public void stop(BundleContext context) throws Exception{
		instance = null;
		super.stop(context);
	}
}
